#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests
import json


class ApiHelper_HeXin:
    """
    和欣数据对接
    """

    def __init__(self, appId, protocol, domain_name):
        self.appId = appId  # 和欣分配的appId
        self.protocol = protocol  # 协议类型
        self.domain_name = domain_name  # 域名
        self.base_url = f'{self.protocol}://{self.domain_name}/_webtalk/_cur/api/'

    @property
    def token(self):
        """
        获取token
        :return:
        """
        url = f'{self.base_url}access_token?appId={self.appId}&grant_type=authorization'
        response = requests.get(url)
        access_token = response.json()['access_token']

        return access_token

    def getCodesVal(self, body, prop=None):
        """
        获取和欣获取实时数据
        :param prop: 点位属性。点位最多查询5个属性，参数格式为"0,1,2,3",如果此参数存在返回结果里面有prop字段 ,属性不存在则为null
        :param body: {"codes":["C_9005_VR_0000","C_9008_VR_0000"]}
        :return:
        """
        url = f'{self.base_url}getCodesVal'
        if prop:
            headers = {'appId': self.appId, 'Token': self.token, 'prop': prop}
        else:
            headers = {'appId': self.appId, 'Token': self.token}
        response = requests.post(url, headers=headers, data=body)
        if response.status_code == 200:
            response = response.json()
            if response['code'] == 200:
                return response['ResultPointObjArr']
            raise ValueError(response['msg'])
        raise ValueError("接口请求失败")

    def getCodesHisVal(self, body, prop=None):
        """
        获取和欣获取历史数据
        //开始结束时间不能超过2个小时
        :param prop: 点位属性。点位最多查询5个属性，参数格式为"0,1,2,3",如果此参数存在返回结果里面有prop字段 ,属性不存在则为null
        :param body: {"codes":["C_5001_AV_3001683","C_5001_AV_3001684"],"starttime":"2020-06-01 23:00:00","endtime":"2020-06-02 01:00:00}
        :return:
        """
        url = f'{self.base_url}getCodesHisVal'
        if prop:
            headers = {'appId': self.appId, 'Token': self.token, 'prop': prop}
        else:
            headers = {'appId': self.appId, 'Token': self.token}
        response = requests.post(url, headers=headers, data=json.dumps(body))
        if response.status_code == 200:
            response = response.json()
            if response['code'] == 200:
                return response['ResultPointObjArr']
            raise ValueError(response['msg'])
        raise ValueError("接口请求失败")


config = {
    "appId": 'FCYY',
    "protocol": "http",
    "domain_name": "192.168.10.235"
}

ApiHelper_HeXin = ApiHelper_HeXin(**config)


def release_result(code_list):
    body = {"codes": code_list}
    ResultPointObjArr_list = ApiHelper_HeXin.getCodesVal(body)
    to_list = []
    for result in ResultPointObjArr_list:
        to_list.append({
            "pointId": result['pointId'],
            "value": result['value'],  # 显示温度单位ug/m3
        })
    return to_list


# if __name__ == '__main__':
#
#     def get_toilet_status():
#         # 卫生间有人无人获取
#         code_list = ["C_1001_BV_0000",
#                      "C_1001_BV_0001",
#                      "C_1001_BV_0002",
#                      "C_1001_BV_0003",
#                      "C_1001_BV_0004",
#                      "C_1001_BV_0005",
#                      "C_1001_BV_0006",
#                      "C_1001_BV_0007",
#                      "C_1001_BV_0008",
#                      "C_1001_BV_0009",
#                      "C_1001_BV_0010",
#                      "C_1001_BV_0011",
#                      "C_1001_BV_0012",
#                      "C_1001_BV_0013",
#                      "C_1001_BV_0014",
#                      "C_1001_BV_0015",
#                      "C_1001_BV_0016",
#                      "C_1001_BV_0017",
#                      "C_1001_BV_0018",
#                      "C_1001_BV_0019",
#                      "C_1001_BV_0200",
#                      "C_1001_BV_0201",
#                      "C_1001_BV_0202",
#                      "C_1001_BV_0203",
#                      "C_1001_BV_0204",
#                      "C_1001_BV_0205",
#                      "C_1001_BV_0206",
#                      "C_1001_BV_0207",
#                      "C_1001_BV_0208",
#                      "C_1001_BV_0209",
#                      "C_1001_BV_0210",
#                      "C_1001_BV_0211",
#                      "C_1001_BV_0212",
#                      "C_1001_BV_0213",
#                      "C_1001_BV_0214",
#                      "C_1001_BV_0215",
#                      "C_1001_BV_0216",
#                      "C_1001_BV_0217",
#                      "C_1001_BV_0218",
#                      "C_1001_BV_0219",
#                      "C_1001_BV_0220",
#                      "C_1001_BV_0221",
#                      "C_1001_BV_0222",
#                      "C_1001_BV_0223",
#                      "C_1001_BV_0224",
#                      "C_1001_BV_0400",
#                      "C_1001_BV_0401",
#                      "C_1001_BV_0402",
#                      "C_1001_BV_0403",
#                      "C_1001_BV_0404",
#                      "C_1001_BV_0405",
#                      "C_1001_BV_0406",
#                      "C_1001_BV_0407",
#                      "C_1001_BV_0408",
#                      "C_1001_BV_0409",
#                      "C_1001_BV_0410",
#                      "C_1001_BV_0411",
#                      "C_1001_BV_0412",
#                      "C_1001_BV_0413",
#                      "C_1001_BV_0414",
#                      "C_1001_BV_0415",
#                      "C_1001_BV_0416",
#                      "C_1001_BV_0417",
#                      "C_1001_BV_0418",
#                      "C_1001_BV_0419",
#                      "C_1001_BV_0420",
#                      "C_1001_BV_0421",
#                      "C_1001_BV_0422",
#                      "C_1001_BV_0423",
#                      "C_1001_BV_0424"]
#         body = {"codes": code_list}
#         ResultPointObjArr_list = ApiHelper_HeXin.getCodesVal(body)
#         to_list = []
#         for result in ResultPointObjArr_list:
#             to_list.append({
#                 "pointId": result['pointId'],
#                 "value": "无人" if result['value'] == 0 else "有人",
#                 "time": result['time']
#             })
#         print(to_list)
#
#
#     def get_shidu_value():
#         # 获取室内湿度
#
#         code_list = ["C_1002_AV_0901", "C_1002_AV_0906", ]
#         return release_result(code_list)
#
#
#     def get_wendu_value():
#         # 获取室内温度
#
#         code_list = ["C_1002_AV_0902", "C_1002_AV_0907", ]
#         return release_result(code_list)
#
#
#     def get_yacha_value():
#         # 获取压差值
#         code_list = ["C_1002_AI_0000", "C_1002_AI_0001", "C_1002_AI_0002"]
#         return release_result(code_list)
#
#
#     def get_pm2_5_value():
#         # 获取PM2.5值
#         code_list = ["C_1002_AV_0900", "C_1002_AV_0905", ]
#         return release_result(code_list)
#
#
#     def get_liuhuaqin_value():
#         # 硫化氢
#         code_list = ["C_1002_AV_0903", "C_1002_AV_0908", ]
#         return release_result(code_list)
#
#
#     def get_anqi_value():
#         # 氨气 ppm
#         code_list = ["C_1002_AV_0904", "C_1002_AV_0909", ]
#         return release_result(code_list)
#
#
#     get_toilet_status()
#     get_shidu_value()
#     get_wendu_value()
#     get_yacha_value()
#     get_pm2_5_value()
#     get_liuhuaqin_value()
#     get_anqi_value()
